//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace StoreNuke.StarterKit.Commerce.StoreControls
{
	/// <summary>
	///		Summary description for Reviews.
	/// </summary>
	public class ReviewAdmin : StoreControl
	{
		protected Panel pnReviewDetails;
		protected Label lblComment;
		protected TextBox txtComment;
		protected Label lblAuthorized;
		protected CheckBox cbAuthorized;
		protected LinkButton btnSave;
		protected Label lblSpace;
		protected LinkButton btnCancel;
		protected Label lblCategory;
		protected Label lblProduct;
		protected DropDownList ddlCategory;
		protected DropDownList ddlProduct;
		protected DataGrid dgReviewList;
		protected string redirectURL;
		protected int reviewID;

		protected ProductsDB products = new ProductsDB();
		protected System.Web.UI.WebControls.Panel pnReviewList;
		protected ReviewsDB review = new ReviewsDB();

		private void Page_Load(object sender, EventArgs e)
		{
			//Get page URL
			redirectURL = "Default.aspx?scid=" + StoreControlID.ToString() + "&sctl=1";

			if(Request.QueryString["rid"] != null)
			{
				reviewID = Convert.ToInt32(Request.QueryString["rid"]);
			}

			if (Page.IsPostBack != true) 
			{
				BindGrid(true);
				BindCategories();

				CheckEditRequest();
			}
		}

		private void CheckEditRequest()
		{
			if(Request.QueryString["redit"] != null)
			{
				if(Request.QueryString["redit"].ToString() == "delete")
				{
					DeleteReview();
				}
				else
				{
					GetReviewInfo();
				}
			}
		}

		private void DeleteReview()
		{
			review.DeleteReview(reviewID);
			Response.Redirect(redirectURL);
		}

		private void GetReviewInfo()
		{
			SqlDataReader dr = review.GetSingleReview(reviewID);
			if(dr.Read())
			{
				txtComment.Text = dr["Comments"].ToString();
				cbAuthorized.Checked = (bool)dr["Authorized"];
			}
			dr.Close();
			SetDetailsPanelVisible(true);
		}

		private void BindGrid(bool bindAll)
		{
			if(bindAll)
			{
				dgReviewList.DataSource = review.GetAllReviews(false);
				dgReviewList.DataBind();
			}
			else
			{
				dgReviewList.DataSource = review.GetReviews(Convert.ToInt32(ddlProduct.SelectedValue));
				dgReviewList.DataBind();
			}
		}

		private void BindCategories()
		{
			ddlCategory.DataSource = products.GetAllProductCategories();
			ddlCategory.DataBind();
			ddlCategory.Items.Insert(0,new ListItem("<Select A Category>","0"));
			BindProducts();
		}

		private void BindProducts()
		{
			ddlProduct.DataSource = products.GetAllProducts(Convert.ToInt32(ddlCategory.SelectedValue));
			ddlProduct.DataBind();
			ddlProduct.Items.Insert(0,new ListItem("<Select A Product>","0"));
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			this.ddlCategory.SelectedIndexChanged += new System.EventHandler(this.ddlCategory_SelectedIndexChanged);
			this.ddlProduct.SelectedIndexChanged += new System.EventHandler(this.ddlProduct_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindProducts();
		}

		private void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindGrid(false);
		}

		public string GetEditURL(int reviewID)
		{
			return "../" + redirectURL + "&redit=edit&rid=" + reviewID;
		}

		public string GetDeleteURL(int reviewID)
		{
			return "../" + redirectURL + "&redit=delete&rid=" + reviewID;
		}

		private void SetDetailsPanelVisible(bool visible)
		{
			pnReviewDetails.Visible = visible;
			pnReviewList.Visible = (!visible);
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			review.UpdateReview(
				reviewID,
				txtComment.Text,
				cbAuthorized.Checked);
			Response.Redirect(redirectURL);
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(redirectURL);
		}
	}
}
